package com.sc.nft.admin.controller;

import com.alibaba.excel.EasyExcel;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.entity.dto.UserPropsRewardsQueryDTO;
import com.sc.nft.entity.vo.UserPropsRewardsSummaryVO;
import com.sc.nft.entity.vo.UserPropsRewardsVO;
import com.sc.nft.service.MallUserPropsRewardsService;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/props/rewards")
@Api(tags = "道具奖励汇总模块")
public class MallUserPropsRewardsController {

    private final MallUserPropsRewardsService mallUserPropsRewardsService;

    @PostMapping("/page")
    @ApiOperation("分页查询用户道具奖励记录")
    public PageResult<UserPropsRewardsVO> page(@RequestBody UserPropsRewardsQueryDTO queryDTO) {
        return PageResult.success(mallUserPropsRewardsService.page(
                queryDTO.getStartTime(),
                queryDTO.getEndTime(),
                queryDTO.getPhoneNumber(),
                queryDTO.getPropsIds(),
                queryDTO.toMp()));
    }

    @PostMapping("/summary")
    @ApiOperation("查询用户道具奖励汇总数据")
    public Result<UserPropsRewardsSummaryVO> getSummary(@RequestBody UserPropsRewardsQueryDTO queryDTO) {
        return Result.success(mallUserPropsRewardsService.getSummary(
                queryDTO.getStartTime(),
                queryDTO.getEndTime(),
                queryDTO.getPhoneNumber(),
                queryDTO.getPropsIds()));
    }

    @SneakyThrows
    @PostMapping(value = "/export", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "用户道具奖励记录导出excel", notes = "用户道具奖励记录导出excel", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public void export(@RequestBody UserPropsRewardsQueryDTO queryDTO, HttpServletResponse response) {
        queryDTO.setPageSize(Integer.MAX_VALUE);
        List<UserPropsRewardsVO> data = mallUserPropsRewardsService.page(
                queryDTO.getStartTime(),
                queryDTO.getEndTime(),
                queryDTO.getPhoneNumber(),
                queryDTO.getPropsIds(),
                queryDTO.toMp()).getRecords();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("用户道具奖励记录", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), UserPropsRewardsVO.class).sheet("用户道具奖励记录").doWrite(data);
    }
}
